package huanghui.netstorage.service;

import java.util.List;

import huanghui.netstorage.dao.UserDAO;
import huanghui.netstorage.dao.VirtualFileDAO;
import huanghui.netstorage.modle.User;
import huanghui.netstorage.modle.VirtualFile;
import huanghui.netstorage.util.NetStorage;

public class UserService 
{
	public int regedit(User user)
	{
		
		UserDAO dao = new UserDAO();
		
		
		List list =dao.findByName(user.getName());
		
		if (list == null || list.isEmpty()) 
		{
			
		}
		else
		{
			return NetStorage.ACK_USER_EXIST;
		}
		
		list = dao.findByEmail(user.getEmail());
		if (list == null || list.isEmpty()) 
		{
			
		}
		else
		{
			return NetStorage.ACK_EMAIL_EXIST;
		}
		
		
		
		int result = dao.save(user);
		if (result != NetStorage.ACK_FAIL) 
		{
			user = (User) dao.findByExample(user).get(0);
			VirtualFile root = new VirtualFile();
			root.setType(1);
			root.setName(user.getName());
			root.setPath("/");
			root.setModifyTime(System.currentTimeMillis());
			root.setSize(0l);
			root.setUser(user);
			result = new VirtualFileDAO().save(root); 
		}
		
		if (result != NetStorage.ACK_FAIL)
		{
			result = NetStorage.ACK_OK;
		}
		return result;
	}
	
	
	public int login(User user)
	{
		int result = NetStorage.ACK_FAIL;
		if (user != null) 
		{
			UserDAO userDAO = new UserDAO();
			List<User> users = userDAO.findByExample(user);
			if (users != null && !users.isEmpty()) 
			{
				result = NetStorage.ACK_OK;
			}
		}
		return result;
	}
	
	public int logout()
	{
		return 0;
	}
	

}
